<template>
  <div class="rightTable">
    <div class="search">
      <p class="float-left mr-10 mb-10" style="width: 200px">
        <el-input v-model="searchParams.account" placeholder="输入名称"></el-input>
      </p>
      <p class="float-left mr-10 mb-10" style="width: 90px">
        <el-select v-model="searchParams.status" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </p>
      <p class="float-left mr-10 mb-10">
        <el-button type="primary" @click="searchUser">搜索</el-button>
        <el-button type="primary" @click="resetUser">重置</el-button>
      </p>
    </div>
    <div class="my-10">
      <bdButton :handlerBtn="handlerBtn"/>
    </div>
    <bdTable :tableHeader="tableHeader" :tableData="tableData" class="mb-10" />
    <bd-page
      :pageSize="pages"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></bd-page>
    <addUserAndEditUser ref="useUser" @update:visible="searchUser(true)"></addUserAndEditUser>
  </div>
</template>
<script>
import addUserAndEditUser from './components/addUserAndEditUser.vue'
import { getUsers, switchUsers, deleteUsers } from '@/api/users'
import { mapState } from 'vuex'
const searchParams = {
  account: null,
  status: null,
  departmentId: null,
  isDy: false
}
export default {
  name: 'rightTable',
  components: {
    addUserAndEditUser
  },
  data () {
    return {
      searchParams: Object.assign({}, searchParams),
      options: [
        {
          value: 0,
          label: '禁用'
        },
        {
          label: '激活',
          value: 1
        }
      ],
      handlerBtn: [
        {
          id: 1,
          name: '新增用户',
          handler: this.addUser
        }
      ],
      tableHeader: {
        type: 'selection',
        header: [
          {
            prop: 'account',
            label: '用户名',
            minWidth: 180
          },
          {
            prop: 'name',
            label: '姓名',
            minWidth: 180
          },
          {
            prop: 'phone',
            label: '电话',
            minWidth: 110
          },
          {
            prop: 'department',
            label: '所属社区',
            minWidth: 180
          },
          {
            prop: 'status',
            label: '状态',
            type: 'switch',
            activeValue: 1,
            inactiveValue: 0,
            handler: this.changeStatus
          },
          {
            prop: 'createTime',
            label: '创建日期',
            minWidth: 150
          },
          {
            label: '操作',
            type: 'button',
            width: 150,
            btn: [
              {
                id: 1,
                name: '编辑',
                handler: this.handleEdit
              },
              {
                id: 2,
                name: '删除',
                type: 'danger',
                handler: this.handleDelete
              }
            ]
          }
        ]
      },
      // 列表数据
      tableData: [],
      // 分页
      pages: {
        page: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  // 计算属性
  computed: {
    ...mapState('system', ['departmentId'])
  },
  watch: {
    departmentId (val) {
      this.searchParams.departmentId = val
      this.searchUser(true)
    }
  },
  mounted () {
    this.searchUser()
  },
  methods: {
    async searchUser (clearPage = false) {
      if (clearPage) {
        this.pages.page = 1
        this.pages.pageSize = 10
      }
      const params = {
        ...this.pages,
        ...this.searchParams
      }
      const { total, list } = await getUsers(params)
      this.tableData = list
      this.total = total
    },
    resetUser () {
      this.searchParams = Object.assign({}, searchParams)
      this.searchUser(true)
    },
    // 新增用户
    addUser () {
      this.$refs.useUser.isShow()
    },
    // 编辑用户
    handleEdit (e, row) {
      this.$refs.useUser.isShow(true, row)
    },
    // 删除用户
    handleDelete (e, row) {
      this.$confirm('此操作将删除用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteUsers({ id: row.id })
        this.searchUser(true)
      }).catch(() => {
      })
    },
    // 切换状态
    changeStatus (row) {
      this.$confirm(`此操作将${row.status ? '启用' : '禁用'}${row.account}用户, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const status = row.status ? 1 : 0
        switchUsers({ id: row.id, status })
      }).catch(() => {
        row.status = row.status ? 0 : 1
      })
    },
    // 条数
    handleSizeChange (val) {
      this.pages.pageSize = val
      this.searchUser()
    },
    // 页数
    handleCurrentChange (val) {
      this.pages.page = val
      this.searchUser()
    }
  }
}
</script>
<style lang="scss" scoped>
.my-10 {
  margin: 10px 0;
}
.mb-10 {
  margin-bottom: 10px;
}
.search {
  display: flex;
  flex-wrap: wrap;
}
.float-left {
  float: left;
}
.mr-10 {
  margin-right: 10px;
}
</style>
